package com.example.eos.service;

import com.example.eos.bean.*;
import com.example.eos.mapper.LabelMapper;
import com.example.eos.mapper.PublicLabelMapper;
import com.example.eos.mapper.PublishMapper;
import com.example.eos.mapper.UserMapper;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
//@CacheConfig(cacheNames = "public")
public class PublishService {

    private PublishMapper publishMapper;
    private LabelMapper labelMapper;
    private PublicLabelMapper publicLabelMapper;
    private UserMapper userMapper;

    @Autowired
    public PublishService(PublishMapper publishMapper, LabelMapper labelMapper, PublicLabelMapper publicLabelMapper, UserMapper userMapper){
        this.publishMapper = publishMapper;
        this.labelMapper = labelMapper;
        this.publicLabelMapper = publicLabelMapper;
        this.userMapper = userMapper;
    }

    //查询作品列表
    public ReturnInfo findPublishs(String labelname){
        if(labelname.equals("")){
            return ReturnInfo.info3(publishMapper.findPublishs());
        }else {
            return ReturnInfo.info3(publishMapper.findPublishByLabelname(labelname));
        }
    }

    //查询作品
//    @Cacheable(key ="#p0")
    public ReturnInfo lookPublish(String publishid){
        return ReturnInfo.info2("成功", publishMapper.findByPublishid(publishid));
    }

    //发布作品
    public ReturnInfo creatPublish(Publish publish){
        User user =(User) SecurityUtils.getSubject().getPrincipal();
        publishMapper.save(publish.completPublish(publish, user.getUserid()));
        userMapper.addPublishnum(user.getUserid());
        String [] labelnameArr = publish.getLabelnames().split(" ");
        for (String aLabelnameArr : labelnameArr) {
            if (labelMapper.findByLabelname(aLabelnameArr) == null) {
                Label label = new Label(aLabelnameArr);
                labelMapper.save(label);
            }
            publicLabelMapper.save(new PublicLabel(publish.getPublishid(), labelMapper.findByLabelname(aLabelnameArr).getLabelid()));
            labelMapper.updateLabelUsednum(aLabelnameArr);
        }
        return ReturnInfo.info1("成功");
    }

    //删除作品
    public ReturnInfo detelePublish(String publishid){
        if(publishMapper.findByPublishid(publishid)==null){
            return ReturnInfo.info1("失败");
        }else {
            User user =(User) SecurityUtils.getSubject().getPrincipal();
            publishMapper.deleteByPublishid(publishid);
            userMapper.minusPublishnum(user.getUserid());
            return ReturnInfo.info1("成功");
        }
    }

    //点赞作品
    public ReturnInfo likePublish(String publishid){
        if(publishMapper.findByPublishid(publishid)==null){
            return ReturnInfo.info1("该作品不存在");
        }else {
            publishMapper.updateLikenum(publishid);
            return ReturnInfo.info1("成功");
        }
    }

}
